package com.iceGame;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;

import com.badlogic.gdx.Gdx;


public class Settings {
    public static boolean soundEnabled = true;
    public static boolean musicEnabled = false;
    public static boolean signsEnabled = true;
    public final static String file = "user1.icegame";
    public static ArrayList<Integer> scores = new ArrayList<Integer>();
    public static ArrayList<Boolean> isLock = new ArrayList<Boolean>();
    public final static int levelNum = 16;
    
    public static void load() {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(Gdx.files.external(file).read()));
            soundEnabled = Boolean.parseBoolean(in.readLine());
            musicEnabled = Boolean.parseBoolean(in.readLine());
            signsEnabled = Boolean.parseBoolean(in.readLine());
            String line = "";
            int counter = 1;
            while ((line = in.readLine()) != null){
				if(counter<17){
					scores.add(Integer.parseInt(line));
            	}
				else{
					isLock.add(Boolean.parseBoolean(line));
				}
				counter++;
            }
        } catch (Throwable e) {
           InitializeSettingsFile();
           CreateSettingsInMemory();
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }

    private static void CreateSettingsInMemory() {
        for (int i = 0; i < levelNum ; i++){
            scores.add(new Integer(0));
            isLock.add(new Boolean(true));
        }
        isLock.remove(0);
        isLock.add(0,new Boolean(false));
    }

    private static void InitializeSettingsFile() {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(Gdx.files.external(file).write(false)));
            out.write(Boolean.toString(soundEnabled) + "\n");
            out.write(Boolean.toString(musicEnabled) + "\n");  
            out.write(Boolean.toString(signsEnabled) + "\n");
            for (int i = 0;i < levelNum;i++){
            	out.write(0 + "\n");
            }
            out.write("false\n");
            for (int i = 0;i <levelNum - 1;i++){
            	out.write("true\n");
            }
        } catch (Throwable e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }
		

	public static void save() {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(Gdx.files.external(file).write(false)));
            out.write(Boolean.toString(soundEnabled) + "\n");
            out.write(Boolean.toString(musicEnabled) + "\n");  
            out.write(Boolean.toString(signsEnabled) + "\n");
            for (int i = 0;i < scores.size();i++){
            	out.write(scores.get(i) + "\n");
            }
            for (int i = 0;i < isLock.size();i++){
            	out.write(Boolean.toString(isLock.get(i)) + "\n");
            }
        } catch (Throwable e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }

}